package br.edu.udc.sistemas.ia5.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Vector;

import br.edu.udc.sistemas.ia5.annotation.Column;

public class Factory {

	public static Object[] createByResultSet(ResultSet rst, Class<?> c) throws Exception {
		Collection<Object> result = new Vector<Object>();
		Field fields[] = Reflection.getFields(c);
		int j = 0;
		while (rst.next()) {
			Object obj = c.newInstance();
			for (int i = 0; i < fields.length; i++) {
				Column column = fields[i].getAnnotation(Column.class);
				switch (column.type()) {
				case Column.CHAR:
					fields[i].set(obj,rst.getString(column.name()));
					break;
				case Column.STRING:
					fields[i].set(obj,rst.getString(column.name()));
					break;
				case Column.DATE:
					fields[i].set(obj,rst.getDate(column.name()));
					break;
				case Column.TIME:
					fields[i].set(obj,rst.getTime(column.name()));
					break;
				case Column.DATETIME:
					fields[i].set(obj,rst.getDate(column.name()));
					break;					
				case Column.BOOLEAN:
					fields[i].set(obj,rst.getBoolean(column.name()));
					break;	
				case Column.INTEGER:
					fields[i].set(obj,rst.getInt(column.name()));
					break;	
				case Column.LONG:
					fields[i].set(obj,rst.getLong(column.name()));
					break;	
				case Column.FLOAT:
					fields[i].set(obj,rst.getFloat(column.name()));
					break;				
				default:
					break;
				}
				result.add(obj);
			}
		}		
		return (Object[]) result.toArray(new Object[result.size()]);
	}
	
}
